package reduce.fz.com.reduce.activity;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.GridLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;

import java.util.List;

import de.greenrobot.event.EventBus;
import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.Tag;
import reduce.fz.com.reduce.bean.Video;
import reduce.fz.com.reduce.bean.VideoCard;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.custom.LoadingDialog;
import reduce.fz.com.reduce.custom.NetworkImageView;
import reduce.fz.com.reduce.event.ShowPubVideoEvent;
import reduce.fz.com.reduce.service.FindService;
import reduce.fz.com.reduce.service.VideoService;
import reduce.fz.com.reduce.service.preference.UserPreference;
import reduce.fz.com.reduce.util.HttpUtils;
import reduce.fz.com.reduce.util.QuPaiOper;
import reduce.fz.com.reduce.util.ServiceBuilder;

public class VideoEditDescActivity extends BaseToolBarActivity {

    //文本编辑框
    private EditText mEtComment;
    private VideoCard videoCard;
    private QuPaiOper quPaiOper;
    private VideoService videoService;
    private LoadingDialog dialog = null;
    private RelativeLayout mRlSave;
    //控制编辑框长度
    private TextView mDescLen;
    //取消按钮
    private TextView mCancel;
    private List<Tag> categoryTags;
    private GridLayout mCategoryView;
    private int maxlen=140;
    private FindService findService=ServiceBuilder.builderFindService(null);
    private TextWatcher watcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            setLenDesc();

        }
    };
    public void setLenDesc()
    {
        mDescLen.setText(mEtComment.length()+"/"+mDescLen);
    }



    public void laodategoryTags()
    {
        findService.queryCategoryTags(new AbResponseListener<List<Tag>>(new TypeReference<Result<List<Tag>>>(){}) {
            @Override
            public void error(ServiceException e) {

            }

            @Override
            public void responseInfo(Result<List<Tag>> data) {
                categoryTags=data.getData();
                initCategoyView();

            }
        });
    }

    public void initCategoyView()
    {
        for(int i=0;i<categoryTags.size();i++)
        {
            NetworkImageView imageView=new NetworkImageView(this);
            imageView.setImageUrl(categoryTags.get(i).url, HttpUtils.getImageLoader());
            mCategoryView.addView(imageView);
        }

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        videoCard = (VideoCard) getIntent().getSerializableExtra("videocard");
        setContentView(R.layout.activity_video_edit_desc);
        setToolbarTitle("编辑内容");
        initWidget();
        quPaiOper = QuPaiOper.instance(this, true);
        videoService = ServiceBuilder.builderVideoService(null);
        laodategoryTags();
    }
    @Override
    public void initWidget() {
        mCategoryView=(GridLayout)findViewById(R.id.gltag_category);
        mEtComment=(EditText)findViewById(R.id.et_comment);
        mRlSave=(RelativeLayout)findViewById(R.id.rl_save);
        mCancel=(TextView)findViewById(R.id.tv_cancel);
        mDescLen=(TextView)findViewById(R.id.tv_desc_len);
    }



    public void subVideo(Video video) {
        dialog = new LoadingDialog(this, "提交中");
        dialog.show();
        quPaiOper.submitVideo(video, new QuPaiOper.QuPaiListener() {
            @Override
            public void submitSuccess(Video video) {
                videoCard.video = video;
                submitInfo(videoCard);
            }

            @Override
            public void submitFailed(Video video) {

            }
        });
    }

    public void submitInfo(final VideoCard card) {
        dialog.setMsg("提交信息中");
        videoService.submitVideo(card, new AbResponseListener<VideoCard>(new TypeReference<Result<VideoCard>>() {
        }) {
            @Override
            public void error(ServiceException e) {
                dialog.dismiss();
            }

            @Override
            public void responseInfo(Result<VideoCard> data) {
                dialog.dismiss();
                VideoCard card=data.getData();
                card.user= UserPreference.getUser();
                EventBus.getDefault().post(new ShowPubVideoEvent(card));
                finish();

            }
        });
    }


    @Override
    public void initListener() {
        mRlSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                videoCard.description = mEtComment.getText().toString();
                subVideo(videoCard.video);

            }
        });
        mCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }
}
